package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import pojo.User;
import service.PlayerBusiness;
import service.ServiceFactory;
import util.JsonParser;

@Controller
public class UserTransfer {

	private PlayerBusiness playerBusiness = ServiceFactory.getPlayBusiness();
	private JsonParser jsonParser = new JsonParser();

	@ResponseBody
	@RequestMapping(value = "/register", method = { RequestMethod.POST }, produces = "application/json;charset=UTF-8")
	public String register(@RequestBody String registerJson) {
		User user = jsonParser.parseUser(registerJson);
		String json = playerBusiness.regUser(user.getUserName(), user.getPassword(), user.getEmail());
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "/login", method = { RequestMethod.POST }, produces = "application/json;charset=UTF-8")
	public String login(@RequestBody String loginJson) {
		User user = jsonParser.parseUser(loginJson);
		String json = playerBusiness.findUser(user.getUserName(), user.getPassword());
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "/getUserInfo/{userId}", method = {
			RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public String login(@PathVariable("userId") int userId) {
		String json = playerBusiness.getUser(userId);
		return json;
	}

}
